package com.dwj.auto.comment.controller;


import com.dwj.auto.comment.common.response.PageResponse;
import com.dwj.auto.comment.common.response.Response;
import com.dwj.auto.comment.model.form.StudentForm;
import com.dwj.auto.comment.model.req.StuSearchCriteria;
import com.dwj.auto.comment.model.req.StudentCriteria;
import com.dwj.auto.comment.model.vo.StudentSearchVo;
import com.dwj.auto.comment.model.vo.StudentVo;
import com.dwj.auto.comment.model.vo.UserVo;
import com.dwj.auto.comment.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dwjian
 * @since 2020-01-13
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @GetMapping("/get/list")
    public PageResponse<StudentVo> getList(StudentCriteria pagination){
        return studentService.getList(pagination);
    }

    @GetMapping("/get")
    public Response<StudentVo> get(String id){
        return studentService.get(id);
    }

    @PostMapping("/edit")
    public Response<String> edit(@RequestBody StudentForm form){
        return studentService.edit(form);
    }

    @PostMapping("/delete")
    public Response<String> delete(@RequestBody List<String> ids){
        return studentService.delete(ids);
    }

    @GetMapping("/search/list")
    public PageResponse<StudentSearchVo> searchList(StuSearchCriteria pagination){
        return studentService.searchList(pagination);
    }

    @GetMapping("/hasStudent")
    public Response<Boolean> hasStudent(){
        //查询用户是否添加学生
        UserVo userVo = (UserVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return Response.success(studentService.hasStudent(userVo));
    }
}
